/**
 * 条码设置
 */
function scBarcodeSetting() {
    var com = scCommon.instance();
    var crEle = com.crEle;

    /**
     * 条码属性
     */
    var barcodeItems = [
        { label: "条码文本：", value: "text", type: "input", placeholder: "必填" },
        {
            label: "条码类型：", value: "barcodeType", type: "select", selectedIndex: 0,
            list: [
                "CODE39", "CODE128", "CODE128A", "CODE128B", "CODE128C",
                "EAN2", "EAN5", "EAN8", "EAN13", "UPC", "UPCE",
                "ITF", "ITF14", "MSI", "MSI10", "MSI11", "MSI1010", "MSI1110", 
                "PHARMACODE", "CODABAR", "GENERICBARCODE"
            ]
        },
        {
            label: "条码颜色：", value: "style.color", type: "dropdown", display: "color", component: "scColorpickerSetting",
            param: { color: "#ddd" }
        },
        { label: "条码预览：", value: "barcode_preview", type: "image", style:{
            width: "auto", height: 60, overflow: "hidden"
        }},
    ];

    var box = null;

    /**
     * 创建视图
     * @returns 
     */
    this.createView = function (opt) {
        box = crEle("div", "sc-barcode-setting");
        var data = opt.data ? opt.data : {};
        com.insertNode(barcodeItems, data, box, "sc-barcode-setting-row", (input) => {
            if (!input.addEventListener) return;
            input.addEventListener("change", () => {
                com.getData(box).then((data) => {
                    JsBarcode("img[name='barcode_preview']", data.text, {
                        //选择要使用的条形码类型
                        format: data.barcodeType,
                        //设置条之间的宽度
                        width: 3,
                        //高度
                        height: 60,
                        //是否在条形码下方显示文字
                        displayValue: false,
                        //覆盖显示的文本
                        // text: data.text,
                        //使文字加粗体或变斜体
                        fontOptions: "bold italic",
                        //设置文本的字体
                        font: "fantasy",
                        //设置文本的水平对齐方式
                        textAlign: "left",
                        //设置文本的垂直位置
                        // textPosition: "top",
                        //设置条形码和文本之间的间距
                        textMargin: 5,
                        //设置文本的大小
                        fontSize: 15,
                        //设置条形码的背景
                        background: "#fff",
                        //设置条和文本的颜色
                        lineColor: data.style.color ? data.style.color : "#000",
                        //设置条形码周围的空白边距
                        margin: 0
                    });
                })
            });
        });

        return box;
    }

    /**
     * 获取数据
     * @returns 
     */
    this.getData = function () {
        return new Promise((resolve)=>{
            let p = com.getData(box);

            p.then((d)=>{
                let data = {};
                data.$value = d.barcode_preview;
                delete d.barcode_preview;
                data.$option = d;
    
                resolve(data);
            })
        })
    }
}